Twitter RSS feed, [domdocument.load]: failed to open stream:

Posted by dave1019 on Stack Overflow See other posts from Stack Overflow or by dave1019
Published on 2010-05-19T14:58:52Z Indexed on 2010/05/19 15:00 UTC
Read the original article Hit count: 205

Filed under:
|
|
|

hi

i'm using the following:

<?php
$doc = new DOMDocument();
$doc->load('http://twitter.com/statuses/user_timeline/XXXXXX.rss');
$arrFeeds = array();

foreach ($doc->getElementsByTagName('item') as $node) {
    $itemRSS = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
    );
    array_push($arrFeeds, $itemRSS);
}

for($i=0;$i<=3;$i++) {
    $tweet=substr($arrFeeds[$i]['title'],17); 
    $tweetDate=strtotime($arrFeeds[$i]['date']);
    $newDate=date('G:ia l F Y ',$tweetDate);
    if($i==0) { $b='style="border:none;"'; }
    $tweetsBox.='<div class="tweetbox" ' . $b . '>
    <div class="tweet"><p>' . $tweet . '</p>
    <div class="tweetdate"><a href="http://twitter.com/XXXXXX">@' . $newDate .'</a></div>
    </div>
    </div>';
} 

return $tweetsBox;
?>

to return the 4 most recent tweets from a given timeline (XXXXX is the relevant feed)

It seems to work fine but i've recently been getting the following error sporadically:

PHP error debug Error: DOMDocument::load(http://twitter.com/statuses/user_timeline/XXXXXX.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 502 Bad Gateway

I've read that the above code is dependant on Twitter beign available and I know it gets rather busy sometimes. Is there either a better way of receiving twits, or is there any kind of error trapping i could do to just to display "tweets are currently unavailable..." ind of message rather than causing an error. I'm usnig ModX CMS so any parse error kills the site rather than just ouputs a warning.

thanks.

© Stack Overflow or respective owner

Related posts about dom

Related posts about document